package com.example.smartcitymovie.ui.home;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.util.Log;

import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.bean.NewsKindBean;
import com.example.smartcitymovie.common.TVPFragment;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class NewsActivity extends AppCompatActivity {
    private TVPFragment TVPFNews;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        getSupportActionBar().setTitle("星闻");
        TVPFNews = (TVPFragment) findViewById(R.id.TVPF_News);
        initView();
    }

    private void initView() {
        OkHttpUtils.getCall("/prod-api/api/movie/press/category/list").enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                NewsKindBean newsKindBean = MyApplication.getGson().fromJson(response.body().string(),NewsKindBean.class);
                List<String> menus = new ArrayList<>();
                List<Fragment> fragments = new ArrayList<>();
                for (NewsKindBean.DataBean bean:newsKindBean.getData()
                     ) {
                    menus.add(bean.getName());
                    fragments.add(KindNewsFragment.newInstance(bean.getSort()));
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TVPFNews.setAdapter(getSupportFragmentManager(),getLifecycle(),fragments,menus);
                    }
                });

            }
        });
    }
}